mui.init();
var tab = [
	{id:"index2", url:"include/newIndex.html", title:null},
	{id:"cart", url:"include/cart.html", title:"租赁车"},
	{id:"find", url:"include/find.html", title:"发现"},
	{id:"server", url:"include/server.html", title:"服务"},
	{id:"center", url:"include/center.html", title:"我的"}
],
winHeight = window.innerHeight,
page_style = {
	top:45 + bexta.getStatusHeight() + 'px',
	bottom:"55px"
//	background:'#000'
},aniShow = {},getTabName = function(oid) {
	for( var i = 0, len = tab.length; i < len ; i++ ){
		if( tab[i].id == oid ){
			return tab[i];
		}
	}
};
mui.plusReady(function () {
	var self = bexta.getWebview();
 	for( var index = 0, len = tab.length; index < len ; index++ ){ 
		var tmp = {};
		//双首页模式。第一个已经创建并显示了
		if( index > 0 ){
			if( tab[index].id == 'center' || tab[index].id == 'server' ){
				var 	sub = plus.webview.create(tab[index].url, tab[index].id,{top:"0px",bottom:"55px"});
			}else{
				var 	sub = plus.webview.create(tab[index].url, tab[index].id, page_style);
			}
			sub.hide();
			self.append(sub);
		}else{
//			var 	sub = plus.webview.create(tab[index].url, tab[index].id,{top:"0px",bottom:"55px"});
			tmp[tab[index].id] = true;
			mui.extend(aniShow, tmp);
//			sub.show()
		}
//		self.append(sub);
	}
});

var eventEdit = true;
var activeTab = tab[0].id ;
var header = document.querySelector('#header');
header.style.paddingTop = bexta.getStatusHeight() + 'px';
header.style.height = (44 + bexta.getStatusHeight()) + 'px';
function setHeader(tid){
	//个别的header的处理函数；
}
mui('.mui-bar-tab').on('tap', 'a', function() {
	var oid = D(this).attr('id');
	//重复点击时,取消效果;
	if( oid == activeTab ){
		return false;
	}
	//判断是否为主页，不是的话就要登录
	if( oid != 'index2' ){
		var user = bexta.isLogin(true);
		if(!user){
			bexta.isLogin('','center/login.html');
			return false;
		}else{
			var viewMsg = getTabName(oid);  
			//若是首页,则隐藏header
			if( oid == tab[0].id || oid == 'center' ){
				header.style.display = 'none';
			}else{
				//否则显示header,并且更改header的文字
				header.style.display = 'block';
				header.querySelector('.mui-title').innerHTML = viewMsg.title;
				setHeader(oid);
			}
			//显示
			//若是IOS, 或者 非第一次显示,则直接显示
			if( mui.os.ios || aniShow[oid] ){
				if( !aniShow[oid]  ){
					if( oid != 'index2' ){
						mui.fire(plus.webview.getWebviewById(oid), 'readyVue');
					}
				}
				plus.webview.show(oid);
			}else{
				var temp = {};
				temp[oid] = true;
				mui.extend(aniShow, temp);
				plus.webview.show(oid, 'fade-in', 300);
				if( oid != 'index2' ){
					mui.fire(plus.webview.getWebviewById(oid), 'readyVue');
				}
			}
			//隐藏头部的编辑按钮 -->租赁车页面
			if( oid != 'cart' ){
				document.getElementById('cart-collect').style.display = 'none';
			}else{
				document.getElementById('cart-collect').style.display = eventEdit;
		//		mui.fire(oid,  'checkUser');
			}
			//更改活动选项卡;
			plus.webview.hide(activeTab);
			activeTab = oid;
		}
	}else{
			var viewMsg = getTabName(oid);
			//若是首页,则隐藏header
			if( oid == tab[0].id || oid == 'center' ){
				header.style.display = 'none';
			}else{
				//否则显示header,并且更改header的文字
				header.style.display = 'block';
				header.querySelector('.mui-title').innerHTML = viewMsg.title;
				setHeader(oid);
			}
			//显示
			//若是IOS, 或者 非第一次显示,则直接显示
			if( mui.os.ios || aniShow[oid] ){
				plus.webview.show(oid);
			}else{
				var temp = {};
				temp[oid] = true;
				mui.extend(aniShow, temp);
				plus.webview.show(oid, 'fade-in', 300);
			}
			//隐藏头部的编辑按钮 -->租赁车页面
			if( oid != 'cart' ){
				document.getElementById('cart-collect').style.display = 'none';
			}else{
				document.getElementById('cart-collect').style.display = eventEdit;
		//		mui.fire(oid,  'checkUser');
			}
			//更改活动选项卡;
			plus.webview.hide(activeTab);
			activeTab = oid;
	}
	
});


//租赁车里的事件
var editCollect = document.getElementById('cart-collect');
window.addEventListener('toggleHeadEvent', function(e){
	eventEdit = e.detail.type == 'none' ? 'none' : "block"; 
	editCollect.style.display = e.detail.type;
});
mui('header').on('tap', '#cart-collect', function() {
	var txt = this.innerText.trim();
	var view = plus.webview.getWebviewById('cart');
	if( txt === '编辑'  ){
		this.innerText = '删除';
	}else if( txt === '删除' ) {
		this.innerText = '编辑';
	}
	mui.fire(view, 'toggleEdit',{
		text:txt
	});
});

function ads(){
	console.log(22222222222);
}
//模拟点击事件
window.addEventListener('toggleClick', function(evt){
	var path = evt.detail.path;
	var twos = evt.detail.twos;
	var tabid = document.getElementById(path);
	mui.trigger(tabid, 'tap');
	var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
	if (tabid !== current) {
		current.classList.remove('mui-active');
		tabid.classList.add('mui-active');
	}
	if( twos && twos.id !='' ){
		//二级页面
		//传第参到到二级页面，操作 
		mui.fire(bexta.getWebview(path[0]), 'openPath', {
			path:twos
		});
	}
});
